package com.itheima.reggie.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.common.R;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录功能
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        //禁止空用户名或者空密码登录
        if (employee == null || StrUtil.isEmpty(employee.getUsername()) || StrUtil.isEmpty(employee.getPassword())) {
            return R.error("用户名密码不能为空");
        }
        //以用户名为条件,在数据库中查找该用户数据
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(qw);
        //查询结果为null,则数据库中不存在该用户名
        if (one == null) {
            return R.error("用户名错误");
        }
        //如果用户状态为0,则该用户暂时被禁用
        if (one.getStatus() == 0) {
            return R.error("该账户已被禁用");
        }
        //分别获取输入的密码和查找到数据库中的该账号的密码
        String password = employee.getPassword();
        String onepassword = one.getPassword();
        //对输入的密码进行加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //比对输入的密码和数据中的密码
        if (onepassword.equals(password)) {
            //结果一致,将密码清空后,将用户名放入会话中,并返回登录成功
            one.setPassword(null);
            session.setAttribute("employee", one.getId());
            return R.success(one);
        } else {
            //结果不一致,返回登录失败
            return R.error("密码错误");
        }


    }

    /**
     * 登出功能
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        //清除会话信息
        session.invalidate();
//        session.removeAttribute("employee");
        return R.success(null);
    }

    /**
     * 添加员工功能
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R saveEmployee(@RequestBody Employee employee, HttpSession session) {
        //设置默认信息
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) session.getAttribute("employee"));
//        employee.setUpdateUser((Long) session.getAttribute("employee"));
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(qw);
        if(one !=null){
            return R.error("该账号已存在");
        }
        boolean save = employeeService.save(employee);
        if (save) {
            return R.success("null");
        } else {
            return R.error("新增员工失败");
        }
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R selectByPageAndCondition(Integer page, Integer pageSize, String name){
        //设置分页条件
        IPage<Employee> pageinfo=new Page<>(page,pageSize);

        //按姓名搜索设置模糊查询条件
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(name)){
            qw.like(Employee::getName,name);
        }
        //调用业务层进行查询
        employeeService.page(pageinfo,qw);
        //返回结果
        return R.success(pageinfo);
    }

    /**
     * 修改员工的禁用和启用/修改员工信息数据
     * @param employee
     * @return
     */
    @PutMapping
    public R updateAccountManagement(@RequestBody Employee employee){
        //设置更新时间和操作者
//        Long id = (Long) session.getAttribute("employee");
//        employee.setUpdateUser(id);
//        employee.setUpdateTime(LocalDateTime.now());
        //调用服务层更新数据
        employeeService.updateById(employee);
        return R.success(employee);
    }

    /**
     * 编辑功能回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee==null){
            return R.error("未找到该数据");
        }
        return R.success(employee);
    }
}
